//
//  ClassStructViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/14.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation
import UIKit

private struct XiaoMing {
    var weight = 70
    var height = 175
}

private class Students {
    
    var xiaoMing : XiaoMing?
    var count : Int8?
    
    init(){
        xiaoMing = XiaoMing()
        count = 0
    }
    
}

class ClassStructViewController : BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titles = ["statement","dataType","referenceType","dictionaryCopy","arrayCopy"]
        self.tableView?.reloadData()   
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        switch indexPath.row{
        case 0:
            statement()
        case 1:
            dataType()
        case 2:
            referenceType()
        case 3:
            dictionaryCopy()
        case 4:
            arrayCopy()
        default:
            print("no more methods")
        }
    }
    
    fileprivate func statement(){
        let students = Students()
        print(students.xiaoMing?.weight)
        
        students.xiaoMing?.height = 155
        print(students.xiaoMing?.height)
        
        let 小明 = XiaoMing(weight: 60, height: 170)
        print(小明)
    }
    
    fileprivate func dataType(){
        let xiaoming = XiaoMing(weight: 60, height: 170)
        var laohuang = xiaoming
        laohuang.weight = 50
        laohuang.height = 150
        if xiaoming.weight == laohuang.weight || xiaoming.height == laohuang.height {
            print("struct : laohuang is the same as xiaoming")
        }else {
            print("struct : laohuang is not the same as xiaoming")
        }
    }
    
    fileprivate func referenceType(){
        let xiaoming = Students()
        let laohuang = xiaoming
        laohuang.count = 10
        laohuang.xiaoMing = nil
        if Unmanaged.passUnretained(laohuang).toOpaque() == Unmanaged.passUnretained(xiaoming).toOpaque() {
            print("class : laohuang is the same as xiaoming")
        }else {
            print("class : 2laohuang is not the same as xiaoming")
        }
        
        if laohuang === xiaoming {
            print("laohuang is the same as xiaoming")
        }
    }
    
    fileprivate func dictionaryCopy(){
        let dic1 = ["1":1,"2":2]
        var dic2 = dic1
        dic2["1"] = 111
        print(dic1)
        print(dic2)
    }
    
    fileprivate func arrayCopy(){
        var array1 = [1,2,3,4]
        let array2 = array1
        array1[1] = 222
        print(array1)
        print(array2)
    }
}
